package xyz.yuzegod.megawalls.classes.mythic.lawless;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.LocationUtils;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class MainSkill extends Skill
{
    public MainSkill(final Classes classes) {
        super("挖挖挖", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 4.0;
            }
            case 2: {
                return 5.0;
            }
            case 3: {
                return 6.0;
            }
            case 4: {
                return 7.0;
            }
            case 5: {
                return 8.0;
            }
            default: {
                return 4.0;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7向前冲刺最多§a" + this.getAttribute(level) + "§7格方块,");
            lore.add("   §7能打碎所有你前进方向的所有方块。");
            lore.add("   §7所有被该技能击中的敌人将");
            lore.add("   §7受到§a" + this.getAttribute(level) + "§7点伤害。");
            return lore;
        }
        lore.add(" §8▪ §7向前冲刺最多§8" + this.getAttribute(level - 1) + " ➜ §a" + this.getAttribute(level) + "§7格方块,");
        lore.add("   §7能打碎所有你前进方向的所有方块。");
        lore.add("   §7所有被该技能击中的敌人将");
        lore.add("   §7受到§8" + this.getAttribute(level - 1) + " ➜ §a" + this.getAttribute(level) + "§7点伤害。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final Player player = gamePlayer.getPlayer();
        final Location from = player.getLocation().clone();
        final int level = kitStats.getSkillLevel();
        new BukkitRunnable() {
            int ticks = 0;
            List<Player> damaged = new ArrayList<Player>();
            
            public void run() {
                if (!gamePlayer.isOnline() || player.isDead() || player.getLocation().distance(from) > MainSkill.this.getAttribute(level) || this.ticks >= 60) {
                    player.setVelocity(new Vector(0.0, 0.0, 0.0));
                    this.cancel();
                    return;
                }
                player.setVelocity(player.getEyeLocation().getDirection().multiply(0.8));
                for (final Block block1 : LocationUtils.getSphere(player.getLocation(), 3)) {
                    if (!MegaWalls.getInstance().getGame().isUnbreakable(block1.getLocation()) && ((block1.getType() != Material.FURNACE && block1.getType() != Material.BURNING_FURNACE && block1.getType() != Material.TRAPPED_CHEST) || gamePlayer.isProtectedBlock(block1))) {
                        if (block1.getType() == Material.BEDROCK) {
                            continue;
                        }
                        block1.breakNaturally();
                    }
                }
                for (final Player nearby : MainSkill.this.getNearbyPlayers(player, 5)) {
                    if (this.damaged.contains(nearby)) {
                        continue;
                    }
                    nearby.damage(MainSkill.this.getAttribute(level), (Entity)player);
                    this.damaged.add(nearby);
                }
                ++this.ticks;
            }
        }.runTaskTimer((Plugin)MegaWalls.getInstance(), 0L, 1L);
        return true;
    }
    
    private List<Player> getNearbyPlayers(final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        for (final Player other : PlayerUtils.getNearbyPlayers((Entity)player, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator()) {
                if (GamePlayer.get(player.getUniqueId()).getGameTeam().isInTeam(gameOther)) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
}
